#include "StrategyImpl.h"

#include "StockNamespace.h"
#include "StockTypes.h"

#include "StrategyTree.h"
#include "StrategyNode.h"


USING_STOCK_NS
using namespace std;

void StrategyImpl::SetPrices(vector<StockPrice> prices)
{
	_prices = prices;
}

void StrategyImpl::Calc()
{
	StrategyTree  tree;
	tree.Build(_prices);

	StrategyNode * node = tree.GetMaxProfitNode();
	vector<StrategyNode *> path = tree.GetNodePath(node);

	_maxProfit = node->GetProfit();

	vector<StrategyNode *>::const_reverse_iterator it;
	for (it = path.rbegin(); it != path.rend(); it++)
	{
		_transactions.push_back((*it)->GetTransaction());
	}
}

StockProfit StrategyImpl::GetMaxProfit()
{
	return _maxProfit;
}

string StrategyImpl::GetTransactions()
{
	string transactions = "";

	for (vector<TransactionType>::const_iterator it = _transactions.begin(); it != _transactions.end(); it++)
	{
		if (transactions.length() > 0)
		{
			transactions += ",";
		}
		if ((*it) == BUY)
		{
			transactions += "buy";
		}
		else if ((*it) == SELL)
		{
			transactions += "sell";
		}
		else if ((*it) == COOLDOWN)
		{
			transactions += "cooldown";
		}
		else if ((*it) == PASS)
		{
			transactions += "pass";
		}
	}
	return transactions;
}
